<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-circle-plus-outline"></i> 员工添加
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="form-box">
                <el-form :model="empData" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                    <el-form-item label="员工编号" prop="empId">
                        <el-input v-model="empData.empId"></el-input>
                    </el-form-item>

                    <el-form-item label="出差时间" prop="errandTime">
                        <el-date-picker v-model="empData.errandTime" type="date" ></el-date-picker>
                    </el-form-item>

                    <el-form-item label="出差天数" prop="errandDay">
                        <el-input v-model="empData.errandDay"></el-input>
                    </el-form-item>


                    <el-form-item label="出差地点" prop="errandPlace">
                        <el-input v-model="empData.errandPlace"></el-input>
                    </el-form-item>

                    <el-form-item label="每日补贴" prop="errandSubsidy">
                        <el-input v-model="empData.errandSubsidy"></el-input>
                    </el-form-item>
                    <el-button type="primary" @click="addErrandd">添加出差信息<i class="el-icon-upload el-icon--right"></i></el-button>
                </el-form>
            </div>
        </div>
    </div>
</template>

<script>
    import {addErrand, updateErrand, selectById} from "../../api/errand";
    export default {
        name: "AddErrand",
        data(){
            return{
                empData: {
                    empId:'',
                    errandDay:'',
                    errandPlace:'',
                    errandSubsidy:'',
                    errandTime:''
                },
                rules: {
                    empId:[
                        {required: true, message: '请输入员工id', trigger: 'blur'},
                    ],
                    errandDay:[
                        {required: true, message: '请输入出差天数', trigger: 'blur'},
                    ],
                    errandPlace:[
                        {required: true, message: '请输入出差地点', trigger: 'blur'},
                    ],
                    errandSubsidy:[
                        {required: true, message: '请输入出彩补贴', trigger: 'blur'},
                    ],
                    errandTime:[
                        {required: true, message: '请输入出差时间', trigger: 'blur'},
                    ]
                }
            }
        },
        methods: {
            addErrandd:function()
            {
                this.$refs['ruleForm'].validate((valid) => {
                    if (valid) {
                        selectById(this.empData.empId).then((res) => {
                            console.log(res.data)
                            if(res.data==null || res.data=='')
                            {
                                console.log(res.data)
                                this.$message.error("该员工编号不存在")
                            }
                            else
                            {
                                this.$confirm('确定要添加出差信息吗？', '警告', {
                                    confirmButtonText: '确定',
                                    cancelButtonText: '取消',
                                    type: 'warning'
                                })
                                .then(() => {
                                    const $this = this;
                                    addErrand(this.empData).then((res) => {
                                        $this.$message.success('添加成功');
                                        this.empData.empId = '';
                                        this.empData.errandDay = '';
                                        this.empData.errandPlace = '';
                                        this.empData.errandSubsidy = '';
                                        this.empData.errandTime = '';
                                        this.$router.push('/errand');
                                    });
                                })
                                .catch(() => {});
                            }
                        });
                    }
                });
            }
        }
    }
</script>

<style scoped>

</style>